<?php
/**
 * Created by PhpStorm.
 * User: Raytine
 * Date: 2018/8/30
 * Time: 15:02
 */
namespace PddWechat\Models\User\Dao;

use Dai\Framework\Base\BaseDao;
use PddWechat\Models\User\DataObject\UserWithdrawDo;

/**
 * Class UserPromotionUrlDao
 * @package PddWechat\Models\User\Dao
 * @dataObject(\PddWechat\Models\User\DataObject\UserWithdrawDo)
 */

class WithdrawCashDao extends BaseDao
{
    public function insert( UserWithdrawDo $userWithdrawDo )
    {
        $userWithdrawDo->id =  $this->execute("user.user_withdraw.insert", $userWithdrawDo);
        return $userWithdrawDo;
    }

    public function updateStatusById($id) {
        if(empty($id)) {
            return false;
        }
        $sql = "UPDATE jd.user_withdraw SET status = 1,updateTime = '".date('Y-m-d H:i:s') ."'  WHERE id = " . intval($id);
        $res = $this->execRaw($sql);
        return $res;
    }

    public function selectUserWithdrawList($params) {
        $sql = "SELECT * FROM jd.user_withdraw ORDER BY id DESC LIMIT ".($params->pn-1) * $params->rn . "," . $params->rn;
        $res = $this->queryRaw($sql);
        $output = [];
        if( count($res) > 0 ) {
            foreach ($res as $item) {
                $output[] = $this->trimBase($item);
            }
        }
        return $output;
    }

    public function selectUserWithdrawNum()
    {
        $sql = "SELECT COUNT(*) AS cnt FROM jd.user_withdraw";
        $res = $this->queryRaw($sql);
        if(!empty($res)) {
            return $res[0]['cnt'];
        }else {
            return 0;
        }
    }

    public function selectUserWithdrawDataByUid($uid, $status)
    {
        if($uid <= 0) {
            return 0;
        }
        $sql = "SELECT SUM(withdrawMoney) AS cnt FROM jd.user_withdraw WHERE uid = ".
        intval($uid) . " AND status = " . intval($status);
        $res = $this->queryRaw($sql);
        if(!empty($res)) {
            return $res[0]['cnt'];
        }else {
            return 0;
        }
    }

    public function selectUserWithdrawListByUid($uid) {
        if($uid <= 0) {
            return [];
        }
        $sql = "SELECT * FROM jd.user_withdraw WHERE uid = " . intval($uid) . " ORDER BY id DESC";
        $res = $this->queryRaw($sql);
        $output = [];
        if( count($res) > 0 ) {
            foreach ($res as $item) {
                $output[] = $this->trimBase($item);
            }
        }
        return $output;
    }
}